package com.maint.m

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import androidx.core.app.NotificationCompat

class StreamService : Service() {
    private val binder = StreamBinder()
    private var streamManager: StreamManager? = null

    companion object {
        private const val CHANNEL_ID = "StreamServiceChannel"
        private const val NOTIFICATION_ID = 1
        const val ACTION_START_STREAM = "start_stream"
        const val ACTION_STOP_STREAM = "stop_stream"
        const val EXTRA_RTMP_URL = "rtmp_url"
    }

    inner class StreamBinder : Binder() {
        fun getService(): StreamService = this@StreamService
    }

    override fun onCreate() {
        super.onCreate()
        createNotificationChannel()
        startForeground(NOTIFICATION_ID, createNotification())
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        when (intent?.action) {
            ACTION_START_STREAM -> {
                val rtmpUrl = intent.getStringExtra(EXTRA_RTMP_URL) ?: StreamUtils.DEFAULT_RTMP_URL
                startStreaming(rtmpUrl)
            }
            ACTION_STOP_STREAM -> {
                stopStreaming()
            }
        }
        return START_STICKY
    }

    override fun onBind(intent: Intent): IBinder {
        return binder
    }

    private fun createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val serviceChannel = NotificationChannel(
                CHANNEL_ID,
                "推流服务通道",
                NotificationManager.IMPORTANCE_LOW
            )
            val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(serviceChannel)
        }
    }

    private fun createNotification(): Notification {
        return NotificationCompat.Builder(this, CHANNEL_ID)
            .setContentTitle("推流服务运行中")
            .setContentText("正在进行视频推流")
            .setSmallIcon(R.drawable.ic_add)
            .setOngoing(true)
            .build()
    }

    fun setStreamManager(streamManager: StreamManager) {
        this.streamManager = streamManager
    }

    fun startStreaming(rtmpUrl: String) {
        try {
            streamManager?.startStreaming(rtmpUrl)
        } catch (e: Exception) {
            Log.e("StreamService", "启动推流失败", e)
        }
    }

    fun stopStreaming() {
        try {
            streamManager?.stopStreaming()
        } catch (e: Exception) {
            Log.e("StreamService", "停止推流失败", e)
        }
    }

    fun isStreaming(): Boolean {
        return streamManager?.isStreaming() ?: false
    }

    override fun onDestroy() {
        super.onDestroy()
        try {
            streamManager?.onDestroy()
        } catch (e: Exception) {
            Log.e("StreamService", "销毁流管理器失败", e)
        }
        Log.d("StreamService", "服务已销毁")
    }
}
